<?php
namespace App\Http\Controllers;

use App\Exceptions\ValidateException;
use App\Model\Setting;
use App\Repository\Contracts\MemberInterface;
use App\Repository\Contracts\MemberJavaInterface;
use App\Repository\Contracts\SettingInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 19:43
 *
 * @Description: 会员
 */
class MemberController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var MemberInterface
     */
    protected $member;

    /**
     * @var MemberJavaInterface
     */
    protected $memberJava;

    protected $uid;

    protected $storeId;

    /**
     * MemberController constructor.
     * @param Request $request
     * @param MemberInterface $member
     * @param MemberJavaInterface $memberJava
     */
    public function __construct(Request $request, MemberInterface $member, MemberJavaInterface $memberJava)
    {
        $this->request = $request;

        $this->member = $member;

        $this->memberJava = $memberJava;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];

        $this->storeId = CommonHelper::getToken(Auth::user())['storeId'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $keywords = $this->request->input('keywords', null);
        //Search Map
        $searchMap = $this->searchMap([
            ['number', $keywords, 'like'],
            ['phone', $keywords, 'like'],
            ['real_name', $keywords, 'like']
        ]);
        $list = $this->member->lists($limit, $page, $searchMap);

        return $this->success($list);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $detail = $this->member->detail($id);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function findByPhone()
    {
        $phone = $this->request->input('phone');
        $detail = $this->member->findByPhone($phone);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function findByNumber()
    {
        $number = $this->request->input('number');
        $detail = $this->member->findByNumber($number);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data = $this->request->only(['phone', 'number', 'real_name', 'memo', 'status']);
            $data['store_id'] = $this->storeId;
            $id = $this->member->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * Update
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updated($id)
    {
        try {
            $data = $this->request->only(['phone', 'number', 'real_name', 'memo', 'status']);
            $result = $this->member->updated($id, $data);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function initMember()
    {
        if (!env('DIRECT_SALE') || !env('JAVA_NETWORK')) {
            return $this->success();
        }

        try {
            $detail = $this->member->findIdForFirst();
            $result = $this->memberJava->syncList(isset($detail[0]) ? $detail[0]['created_at'] : 0);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function ticket()
    {
        $uid = $this->request->input('uid', null);
        $detail = $this->member->detail($uid, true);
        $detail['storeConfig'] = json_decode(app(SettingInterface::class)->findByName(Setting::STORE_CONFIG)['config'], true);

        return $this->success($detail);
    }
}